package com.vca.service.service.Impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vca.common.constants.Constants;
import com.vca.common.constants.SysGroupDataConstants;
import com.vca.common.exception.VcaException;
import com.vca.common.model.course.CourseAbout;
import com.vca.common.model.course.CoursePackageRelation;
import com.vca.common.model.exhibition.Exhibition;
import com.vca.common.model.exhibition.ExhibitionInfo;
import com.vca.common.model.exhibition.ExhibitionScheduling;
import com.vca.common.model.system.SystemAttachment;
import com.vca.common.model.user.User;
import com.vca.common.model.user.UserCollection;
import com.vca.common.page.CommonPage;
import com.vca.common.request.*;
import com.vca.common.response.*;
import com.vca.common.utils.VcaUtil;
import com.vca.common.vo.ExhibitionInfoVo;
import com.vca.common.vo.ExhibitionVo;
import com.vca.common.vo.SchedulingVo;
import com.vca.service.dao.exhibition.ExhibitionDao;
import com.vca.service.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 展览表 服务实现类
 * </p>
 *
 * @author chenBing
 * @since 2022-11-03
 */
@Service
public class ExhibitionServiceImpl extends ServiceImpl<ExhibitionDao, Exhibition> implements ExhibitionService {

    @Resource
    private ExhibitionDao dao;

    @Autowired
    private SystemGroupDataService systemGroupDataService;

    @Autowired
    private ExhibitionInfoService exhibitionInfoService;

    @Autowired
    private ExhibitionSchedulingService exhibitionSchedulingService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserCollectionService userCollectionService;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private SystemAttachmentService systemAttachmentService;

    @Autowired
    private CourseAboutService courseAboutService;

    @Autowired
    private VcaOrderService vcaOrderService;

    @Autowired
    private SystemConfigService systemConfigService;

    @Autowired
    private VcaLineUpService vcaLineUpService;

    /**
     * @Description:展览分页列表
     * @Author: chenBing
     * @Date: 2022/11/3
     */
    @Override
    public PageInfo<ExhibitionVo> getList(PageParamRequest pageParamRequest, String date) {
        LambdaQueryWrapper<Exhibition> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Exhibition::getStatus, 1).eq(Exhibition::getIsRecycle, 0).eq(Exhibition::getIsDeleted, 0).eq(Exhibition::getAppointmentStatus, 1);
        List<Exhibition> exhibitionList = list(lqw);
        List<ExhibitionResponse> exhibitionResponses = new ArrayList<>();
        if (exhibitionList.size() <= 0) {
            return fenYe(exhibitionResponses, pageParamRequest);
        }
        for (Exhibition exhibition : exhibitionList) {
            ExhibitionResponse exhibitionResponse = info("",exhibition.getId(), "info_list", null);
            if (exhibitionResponse == null) {
                continue;
            }
            exhibitionResponses.add(exhibitionResponse);
        }
        return fenYe(exhibitionResponses, pageParamRequest);
    }

    @Override
    public List<ExhibitionResponse> getListCalendar(String date) {
        LambdaQueryWrapper<Exhibition> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Exhibition::getStatus, 1).eq(Exhibition::getIsRecycle, 0).eq(Exhibition::getIsDeleted, 0).eq(Exhibition::getAppointmentStatus, 1);
        if (StringUtils.isNotBlank(date)) {
            List<ExhibitionScheduling> schedulingList = exhibitionSchedulingService.list(new LambdaQueryWrapper<ExhibitionScheduling>()
                    .eq(ExhibitionScheduling::getIsDeleted, false)
                    .eq(ExhibitionScheduling::getStatus, true).eq(ExhibitionScheduling::getIsCancel, false)
                    .eq(ExhibitionScheduling::getSchedulingDate, date)
                    .ge(ExhibitionScheduling::getQuota, 0));
            List<ExhibitionScheduling> schedulingResult = new ArrayList<>();
            schedulingList.forEach(scheduling -> {
                if (com.vca.common.utils.DateUtil.getSchedulingStatus(scheduling.getSchedulingDate(), scheduling.getSchedulingStartTime(), scheduling.getSchedulingEndTime()) != 0) {
                    schedulingResult.add(scheduling);
                }
            });
            schedulingList.retainAll(schedulingResult);
            List<Long> exhibitionIds = new ArrayList<>();
            if (schedulingList.size() > 0) {
                schedulingList.forEach(s -> {
                    exhibitionIds.add(s.getExhibitionId());
                });
                lqw.in(Exhibition::getId, exhibitionIds);
            }
        }
        List<Exhibition> exhibitionList = list(lqw);
        List<ExhibitionResponse> exhibitionResponses = new ArrayList<>();
        if (exhibitionList.size() <= 0) {
            return null;
        }
        for (Exhibition exhibition : exhibitionList) {
            ExhibitionResponse exhibitionResponse = info("",exhibition.getId(), "info_list", date);
            if (exhibitionResponse == null) {
                continue;
            }
            exhibitionResponses.add(exhibitionResponse);
        }
        return exhibitionResponses;
    }

    /**
     * @Description:处理分页
     * @Author: chenBing
     * @Date: 2022/11/3
     */
    private PageInfo<ExhibitionVo> fenYe(List<ExhibitionResponse> exhibitionResponses, PageParamRequest pageParamRequest) {
        exhibitionResponses = exhibitionResponses.stream()
                .sorted(Comparator.comparing(ExhibitionResponse::getSchedulingDate)
                        .thenComparing(Comparator.comparing(ExhibitionResponse::getSchedulingStartTime))
                        .thenComparing(Comparator.comparing(ExhibitionResponse::getSchedulingSeatCount).reversed())).collect(Collectors.toList());
        List<ExhibitionVo> exhibitionVoList = new ArrayList<>();
        exhibitionResponses.forEach(exhibitionResponse -> {
            ExhibitionVo exhibitionVo = new ExhibitionVo();
            exhibitionVo.setSchedulingList(getSechdulingLists(exhibitionVo, exhibitionResponse));
            exhibitionVoList.add(exhibitionVo);
        });
        return startPage(exhibitionVoList, pageParamRequest.getPage(), pageParamRequest.getLimit());
    }

    /**
     * @Description:处理分页返回的responseVo
     * @Author: chenBing
     * @Date: 2022/11/15
     */
    private List<ExhibitionVo.Sechduling> getSechdulingLists(ExhibitionVo exhibitionVo, ExhibitionResponse exhibitionResponse) {
        List<ExhibitionVo.Sechduling> sechdulingList = new ArrayList<>();
        BeanUtils.copyProperties(exhibitionResponse, exhibitionVo);

        System.out.println(exhibitionVo+"##################");

        exhibitionVo.setExhibitionInfo(exhibitionResponse.getExhibitionInfo());
        exhibitionVo.setEventRecommendations(exhibitionResponse.getEventRecommendations());
        exhibitionVo.setShopRecommendations(exhibitionResponse.getShopRecommendations());
        exhibitionVo.setPublicationRecommendations(exhibitionResponse.getPublicationRecommendations());
        exhibitionVo.setStartTime(DateUtil.format(exhibitionResponse.getStartTime(), Constants.DATE_FORMAT_DATE));
        exhibitionVo.setEndTime(DateUtil.format(exhibitionResponse.getEndTime(), Constants.DATE_FORMAT_DATE));
        exhibitionResponse.getSchedulingList().forEach(exhibitionScheduling -> {
            ExhibitionVo.Sechduling sechduling = new ExhibitionVo.Sechduling();
            sechduling.setDate(DateUtil.format(com.vca.common.utils.DateUtil.strToDate(exhibitionScheduling.getSchedulingDate(),
                    Constants.DATE_FORMAT_DATE), Constants.DATE_FORMAT_DATE));
            if (checkSchedulingVo(sechduling, sechdulingList)) {
                sechdulingList.add(sechduling);
            }
        });
        User user = userService.getInfo();
        // TODO: 2022/11/28 判断当前课程是否被当前用户收藏
        if (user != null) {
            boolean collect = userCollectionService.count(new LambdaQueryWrapper<UserCollection>().eq(UserCollection::getUid, user.getUid())
                    .eq(UserCollection::getMainType, Constants.ORDER_TYPE_EXHIBITION).eq(UserCollection::getMainId, exhibitionResponse.getId())
                    .eq(UserCollection::getType, "collect").eq(UserCollection::getIsDeleted, false)) > 0;
            exhibitionVo.setIsCollection(collect);
        }
        Integer totalSeatsLeftForAllScheduledExhibition = 0;
        for (ExhibitionVo.Sechduling sechduling : sechdulingList) {
            List<ExhibitionVo.Sechduling.TimeSlots> timeSlotsList = new ArrayList<>();
            for (ExhibitionScheduling exhibitionScheduling : exhibitionResponse.getSchedulingList()) {
                ExhibitionVo.Sechduling.TimeSlots timeSlots = new ExhibitionVo.Sechduling.TimeSlots();
                String schedulingDate = DateUtil.format(com.vca.common.utils.DateUtil.strToDate(exhibitionScheduling.getSchedulingDate(), Constants.DATE_FORMAT_DATE), Constants.DATE_FORMAT_DATE);
                if (sechduling.getDate().equals(schedulingDate)) {
                    timeSlots.setSechdulingId(exhibitionScheduling.getId());
                    timeSlots.setPrice(exhibitionScheduling.getPrice());
                    timeSlots.setTimeStart(exhibitionScheduling.getSchedulingStartTime());
                    timeSlots.setTimeEnd(exhibitionScheduling.getSchedulingEndTime());
                    timeSlots.setIsDel(exhibitionScheduling.getIsDeleted() != 0);
                    timeSlots.setRemainingSeatCount(exhibitionScheduling.getQuota());
                    timeSlots.setSchedulingStatus(com.vca.common.utils.DateUtil.getSchedulingStatus(exhibitionScheduling.getSchedulingDate(), exhibitionScheduling.getSchedulingStartTime(), exhibitionScheduling.getSchedulingEndTime()));

                    // TODO: 2022/12/6 判断当前预约是否已截止
                    Integer courseEndOrderTimeHour = Integer.valueOf(systemConfigService.getValueByKey(Constants.EXHIBITION_END_ORDER_TIME));
                    DateTime offset = cn.hutool.core.date.DateUtil.offset(com.vca.common.utils.DateUtil.strToDate(
                            exhibitionScheduling.getSchedulingDate() + " " + exhibitionScheduling.getSchedulingStartTime() + ":00", "yyyy-MM-dd HH:mm:ss"), DateField.HOUR, -courseEndOrderTimeHour);
                    long between = cn.hutool.core.date.DateUtil.between(offset, cn.hutool.core.date.DateUtil.date(), DateUnit.SECOND, false);
                    if (between >= 0) {
                        timeSlots.setIsStopAppointment(false);
                    }
                    timeSlotsList.add(timeSlots);
                }
            }
            sechduling.setTimeSlots(timeSlotsList);
        }
        Integer orderQuantityToBeWrittenOff = 0;
        Integer completedOrderQuantity = 0;
        List<ExhibitionScheduling> schedulingList = exhibitionSchedulingService.list(new LambdaQueryWrapper<ExhibitionScheduling>().eq(ExhibitionScheduling::getExhibitionId, exhibitionVo.getId()));
        if (schedulingList.size() > 0) {
            // TODO: 2022/11/28 判断展览是否预约过
            List<Long> schedulingIds = schedulingList.stream().map(ExhibitionScheduling::getId).collect(Collectors.toList());
            if (user != null) {
                HashMap<String, Object> map = vcaOrderService.checkPersonAppointmentExhibitionBySchedulingId(user, exhibitionResponse, schedulingIds, Constants.ORDER_TYPE_EXHIBITION);
                orderQuantityToBeWrittenOff = (Integer) map.get("orderQuantityToBeWrittenOff");
                completedOrderQuantity = (Integer) map.get("completedOrderQuantity");
            }
            totalSeatsLeftForAllScheduledExhibition = schedulingList.stream().mapToInt(ExhibitionScheduling::getQuota).sum();
        }
        exhibitionVo.setIsAppointment(orderQuantityToBeWrittenOff > 0 || completedOrderQuantity > 0 ? true : false);
        exhibitionVo.setOrderQuantityToBeWrittenOff(orderQuantityToBeWrittenOff);
        exhibitionVo.setCompletedOrderQuantity(completedOrderQuantity);
        exhibitionVo.setTotalSeatsLeftForAllScheduledExhibition(totalSeatsLeftForAllScheduledExhibition);
        if (sechdulingList.size() > 0) {
            List<ExhibitionVo.Sechduling.TimeSlots> timeSlots = sechdulingList.get(0).getTimeSlots();
            if (timeSlots.size() > 0) {
                exhibitionVo.setPrice(timeSlots.get(0).getPrice());
            }
        }
        System.out.println(sechdulingList+"&&&&&&&&&&&&&&&&&&&&");
        return sechdulingList;
    }

    /**
     * @Description:判断是重复
     * @Author: chenBing
     * @Date: 2022/11/15
     */
    private boolean checkSchedulingVo(ExhibitionVo.Sechduling sechduling, List<ExhibitionVo.Sechduling> sechdulingList) {
        boolean isCheck = true;
        for (ExhibitionVo.Sechduling s : sechdulingList) {
            if (s.getDate().equals(sechduling.getDate())) {
                isCheck = false;
            }
        }
        return isCheck;
    }

    /**
     * @Description:分页
     * @Author: chenBing
     * @Date: 2022/11/3
     */
    public <T> PageInfo<T> startPage(List<T> list, Integer pageNum, Integer pageSize) {
        //创建Page类
        com.github.pagehelper.Page page = new com.github.pagehelper.Page(pageNum, pageSize);
        //为Page类中的total属性赋值
        page.setTotal(list.size());
        //计算当前需要显示的数据下标起始值
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, list.size());
        //从链表中截取需要显示的子链表，并加入到Page
        if (startIndex < endIndex) {
            page.addAll(list.subList(startIndex, endIndex));
        }
        //以Page创建PageInfo
        PageInfo pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    /**
     * @Description:展览详情
     * @Author: chenBing
     * @Date: 2022/11/3
     */
    @Override
    public ExhibitionVo info(String language,Long exhibitionId) {
        ExhibitionVo exhibitionVo = new ExhibitionVo();
        ExhibitionResponse exhibitionResponse = info(language,exhibitionId, "info_one", null);

        System.out.println(exhibitionResponse+"^^^^^^^^^^^^^^^^^^^^^^");
        exhibitionVo.setSchedulingList(getSechdulingLists(exhibitionVo, exhibitionResponse));
        System.out.println(exhibitionVo+"**********************");
        return exhibitionVo;
    }

    /**
     * 展览管理
     *
     * @param request
     * @param pageParamRequest
     * @return
     * @author Li
     * @date 2022/11/11 13:16
     */
    @Override
    public PageInfo getAdminList(AdminCommonRequest request, PageParamRequest pageParamRequest) {

        Page<ExhibitionAdminListResponse> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<ExhibitionAdminListResponse> exhibitionAdminList = new ArrayList<>();
        LambdaUpdateWrapper<Exhibition> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(Exhibition::getIsDeleted, 0);
        //状态暂定
//        if (ObjectUtil.isNotEmpty(request.getType())){
//            wrapper.eq(Exhibition::get,request.getType());
//        }
        if (ObjectUtil.isNotEmpty(request.getStatus())) {
            wrapper.eq(Exhibition::getStatus, request.getStatus());
        }
        if (ObjectUtil.isNotEmpty(request.getKeywords())) {
            List<Integer> ids = dao.fuzzySearch(request.getKeywords());
            if (ObjectUtil.isEmpty(ids)) {
                return CommonPage.copyPageInfo(page, exhibitionAdminList);
            }
            wrapper.in(Exhibition::getId, ids);
        }
        List<Exhibition> exhibitions = dao.selectList(wrapper);
        Map<String, List<HashMap<String, Object>>> courseAdminConfig = systemGroupDataService.getCourseAdminConfig();
        List<HashMap<String, Object>> address = courseAdminConfig.get("addressList");
        exhibitions.forEach(exhibition -> {
            ExhibitionAdminListResponse response = new ExhibitionAdminListResponse();
            BeanUtils.copyProperties(exhibition, response);
            response.setCover(exhibition.getCover());
            List<String> hours = new ArrayList<>();
            hours.add(exhibition.getStartHours());
            hours.add(exhibition.getEndHours());
            response.setHours(hours);
            response.setPrice(exhibition.getPrice().toString());
            address.forEach(e -> {
                Map map = JSON.parseObject(JSONObject.toJSONString(e), Map.class);
                if (map.get("id").equals(exhibition.getCourseAddressId())) {
                    response.setCourseAddress(map.get("courseAddress").toString() + map.get("courseAddressInfo").toString());
                }
            });
            // TODO: 2022/11/3 设置talkInfo
            ExhibitionInfo exhibitionInfo = exhibitionInfoService.getOne(new LambdaQueryWrapper<ExhibitionInfo>().eq(ExhibitionInfo::getExhibitionId, exhibition.getId()));
            ExhibitionInfoVo exhibitionInfoVo = new ExhibitionInfoVo();
            BeanUtils.copyProperties(exhibitionInfo, exhibitionInfoVo);
            exhibitionInfoVo.setDetails(JSONObject.parseArray(exhibitionInfo.getDetails()));
            exhibitionInfoVo.setDetailsEn(JSONObject.parseArray(exhibitionInfo.getDetailsEn()));
            response.setExhibitionInfo(exhibitionInfoVo);
            response.setCourseType(response.getStartTime() + "~" + response.getEndTime());
            response.setCoverImage(exhibition.getCover());
            exhibitionAdminList.add(response);
        });
        return CommonPage.copyPageInfo(page, exhibitionAdminList.stream().sorted(Comparator.comparing(ExhibitionAdminListResponse::getSort).reversed()).collect(Collectors.toList()));
    }

    /**
     * @Description:详情
     * @Author: chenBing
     * @Date: 2022/11/3
     */
    private ExhibitionResponse info(String language,Long exhibitionId, String type, String date) {
        ExhibitionResponse exhibitionResponse = new ExhibitionResponse();
        Exhibition exhibition = getById(exhibitionId);
        if (exhibition == null) {
            return exhibitionResponse;
        }
        System.out.println(exhibition+"==============");
        BeanUtils.copyProperties(exhibition, exhibitionResponse);
        if (StringUtils.isNotBlank(exhibition.getMainPictureVideo())) {
            SystemAttachment systemAttachment = systemAttachmentService.getOne(new LambdaQueryWrapper<SystemAttachment>().eq(SystemAttachment::getSattDir, exhibition.getMainPictureVideo()));
            exhibitionResponse.setMainPictureVideoWidth(systemAttachment.getAttWidth());
            exhibitionResponse.setMainPictureVideoHeight(systemAttachment.getAttHeight());
        }
        if("en".equals(language)){
            exhibitionResponse.setName(exhibitionResponse.getNameEn());
            exhibitionResponse.setIntroduction(exhibitionResponse.getIntroductionEn());
            exhibitionResponse.setCityName(exhibitionResponse.getCityNameEn());
        }
        HashMap<String, Object> addressMap = systemGroupDataService.getMapByGid(SysGroupDataConstants.GROUP_DATA_ID_CLIENT_COURSE_ADDRESS, exhibition.getCourseAddressId());

        Object courseAddress = "";
        Object courseAddressInfo = "";
        Object dest = "";
        if("en".equals(language)){
            courseAddress = addressMap.get("courseAddressEn");
            courseAddressInfo = addressMap.get("courseAddressInfoEn");
            dest = addressMap.get("destEn");
        }else{
            courseAddress = addressMap.get("courseAddress");
            courseAddressInfo = addressMap.get("courseAddressInfo");
            dest = addressMap.get("dest");
        }

        Object lat = addressMap.get("lat");
        Object lon = addressMap.get("lon");

        HashMap<String, Object> talkAddressInfoMap = new HashMap<>();
        talkAddressInfoMap.put("courseAddress", courseAddress);
        talkAddressInfoMap.put("courseAddressInfo", courseAddressInfo);
        talkAddressInfoMap.put("lat", lat);
        talkAddressInfoMap.put("lon", lon);
        talkAddressInfoMap.put("dest", dest);
        exhibitionResponse.setAddress(talkAddressInfoMap);
        JSONArray courseCarouselMap = JSONArray.parseArray(exhibition.getCarouselMap() == null ? "" : exhibition.getCarouselMap());
        exhibitionResponse.setCarouselMap(courseCarouselMap);

        LambdaQueryWrapper<ExhibitionScheduling> lqw = new LambdaQueryWrapper<>();
        int day = Integer.parseInt(systemConfigService.getValueByKey(Constants.EXHIBITION_SHOW_DAY));
        String dateToTenDayDate = DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, day), "yyyy-MM-dd");
        lqw.eq(ExhibitionScheduling::getStatus, 1)
                .eq(ExhibitionScheduling::getIsDeleted, 0)
                .eq(ExhibitionScheduling::getExhibitionId, exhibition.getId())
                .eq(ExhibitionScheduling::getIsCancel, false)
                .between(ExhibitionScheduling::getSchedulingDate, DateUtil.format(new Date(), Constants.DATE_FORMAT_DATE), dateToTenDayDate)
                .orderByAsc(ExhibitionScheduling::getSchedulingDate)
                .orderByAsc(ExhibitionScheduling::getSchedulingStartTime);
        if (StringUtils.isNotBlank(date)) {
            lqw.eq(ExhibitionScheduling::getSchedulingDate, date)
                    .ge(ExhibitionScheduling::getQuota, 0);
        }
        List<ExhibitionScheduling> exhibitionSchedulingList = exhibitionSchedulingService.list(lqw);
        exhibitionSchedulingList.forEach(exhibitionScheduling -> {
            exhibitionScheduling.setSchedulingSeatCount(exhibitionScheduling.getQuota());
        });
        List<ExhibitionScheduling> exhibitionSchedulingArrayList = new ArrayList<>();
        for (ExhibitionScheduling exhibitionScheduling : exhibitionSchedulingList) {
            if (exhibitionScheduling.getSchedulingDate().compareTo(DateUtil.format(new Date(), "yyyy-MM-dd")) == 0) {
                String format = DateUtil.format(new Date(), "HH:mm");
                if (exhibitionScheduling.getSchedulingStartTime().compareTo(format) <= -1) {
                    exhibitionSchedulingArrayList.add(exhibitionScheduling);
                }
            }
        }
        exhibitionSchedulingList.removeAll(exhibitionSchedulingArrayList);
        exhibitionResponse.setSchedulingList(exhibitionSchedulingList);
        lqw.ge(ExhibitionScheduling::getSchedulingDate, DateUtil.format(DateUtil.beginOfDay(new Date()), Constants.DATE_FORMAT_DATE));
        List<ExhibitionScheduling> exhibitionSchedulings = exhibitionSchedulingService.list(lqw);


        if (exhibitionSchedulings.size() > 0) {
            exhibitionResponse = checkScheduling(exhibitionResponse, exhibitionSchedulings, exhibition);
        } else if ("info_list".equals(type)) {
            return null;
        }
        // TODO: 2022/11/3 设置exhibitionInfoVo
        ExhibitionInfo exhibitionInfo = exhibitionInfoService.getOne(new LambdaQueryWrapper<ExhibitionInfo>().eq(ExhibitionInfo::getExhibitionId, exhibitionId));
        ExhibitionInfoVo exhibitionInfoVo = new ExhibitionInfoVo();
        BeanUtils.copyProperties(exhibitionInfo, exhibitionInfoVo);

        if("en".equals(language)){
            exhibitionInfoVo.setDetails(JSONObject.parseArray(exhibitionInfo.getDetailsEn()));
        }else{
            exhibitionInfoVo.setDetails(JSONObject.parseArray(exhibitionInfo.getDetails()));
        }


        System.out.println(exhibitionResponse+"-------------");
        exhibitionResponse.setExhibitionInfo(exhibitionInfoVo);
        // TODO: 2022/9/23 处理相关课程推荐
        exhibitionResponse.setEventRecommendations(courseService.getCourseAbouts(language,1, 3, Math.toIntExact(exhibitionId)));
        // TODO: 2022/10/18 处理你可能喜欢的商品 推荐
        exhibitionResponse.setShopRecommendations(courseService.getCourseAbouts(language,2, 3, Math.toIntExact(exhibitionId)));
        // TODO: 2022/12/1 处理相关出版物推荐
        exhibitionResponse.setPublicationRecommendations(courseService.getCourseAbouts(language,3, 3, Math.toIntExact(exhibitionId)));
        return exhibitionResponse;
    }

    /**
     * @Description:校验展览排期
     * @Author: chenBing
     * @Date: 2022/11/18
     */
    private ExhibitionResponse checkScheduling(ExhibitionResponse exhibitionResponse, List<ExhibitionScheduling> exhibitionSchedulings, Exhibition exhibition) {
        DateTime date = DateUtil.date();
        SchedulingVo schedulingV = new SchedulingVo();
        schedulingV.setScheduled(0L);
        schedulingV.setScheduledDateTime(date);
        List<SchedulingVo> schedulingVos = new ArrayList<>();
        for (ExhibitionScheduling scheduling : exhibitionSchedulings) {
            SchedulingVo schedulingVo = new SchedulingVo();
            // TODO: 2022/11/25 yyyy-MM-dd HH:mm:ss
            String schedulingDate = scheduling.getSchedulingDate() + " " + scheduling.getSchedulingStartTime();
            DateTime dateTime = DateUtil.parse(schedulingDate, "yyyy-MM-dd HH:mm");
            schedulingVo.setScheduledDateTime(dateTime);
            schedulingVo.setScheduled(scheduling.getId());
            schedulingVos.add(schedulingVo);
        }
        schedulingVos.add(schedulingV);
        schedulingVos = schedulingVos.stream().sorted(Comparator.comparing(SchedulingVo::getScheduledDateTime)).collect(Collectors.toList());
        Long scheduledId = 0L;
        for (int i = 0; i < schedulingVos.size(); i++) {
            if (schedulingVos.get(i).getScheduled() == 0 && i + 1 < schedulingVos.size()) {
                scheduledId = schedulingVos.get(i + 1).getScheduled();
            }
        }
        if (scheduledId == 0) {
            return exhibitionResponse;
        }
        ExhibitionScheduling exhibitionScheduling = exhibitionSchedulingService.getById(scheduledId);
        exhibitionResponse.setSchedulingDate(exhibitionScheduling.getSchedulingDate());
        exhibitionResponse.setSchedulingStartTime(exhibitionScheduling.getSchedulingStartTime());
        exhibitionResponse.setSchedulingEndTime(exhibitionScheduling.getSchedulingEndTime());
        exhibitionResponse.setSchedulingSeatCount(exhibitionScheduling.getQuota());
        return exhibitionResponse;
    }

    /**
     * @Description:展览列表
     * @Author: chenBing
     * @Date: 2022/11/16
     */
    @Override
    public ExhibitionVoResponse getListNoPage(String language) {
        List<ExhibitionVoResponse> exhibitionVoResponses = new ArrayList<>();
        //查询进行中
        List<Exhibition> exhibitionListIng = list(new LambdaQueryWrapper<Exhibition>().eq(Exhibition::getStatus, 1).eq(Exhibition::getAppointmentStatus, 1).orderByDesc(Exhibition::getSort).eq(Exhibition::getIsDeleted, false));
        exhibitionVoResponses = getExhibitions(language,exhibitionListIng);
        //查询未开始
        List<Exhibition> exhibitionListWait = list(new LambdaQueryWrapper<Exhibition>().eq(Exhibition::getStatus, 1).eq(Exhibition::getAppointmentStatus, 2).orderByDesc(Exhibition::getSort).eq(Exhibition::getIsDeleted, false));
        List<ExhibitionVoResponse> exhibitionsWait = getExhibitions(language,exhibitionListWait);
        if (exhibitionsWait.size() > 0) {
            for (ExhibitionVoResponse exhibitionVoResponse : exhibitionsWait) {
                exhibitionVoResponses.add(exhibitionVoResponse);
            }
        }
        //查询已结束
        List<Exhibition> exhibitionListEnd = list(new LambdaQueryWrapper<Exhibition>().eq(Exhibition::getStatus, 1).eq(Exhibition::getAppointmentStatus, 0).orderByDesc(Exhibition::getSort).eq(Exhibition::getIsDeleted, false));
        List<ExhibitionVoResponse> exhibitionsEnd = getExhibitions(language,exhibitionListEnd);
        if (exhibitionsEnd.size() > 0) {
            for (ExhibitionVoResponse exhibitionVoResponse : exhibitionsEnd) {
                exhibitionVoResponses.add(exhibitionVoResponse);
            }
        }
        ExhibitionVoResponse exhibitionVoResponse = new ExhibitionVoResponse();
        if (exhibitionVoResponses.size() > 0) {
            BeanUtils.copyProperties(exhibitionVoResponses.get(0), exhibitionVoResponse);
            List<ExhibitionVoResponse.ListedExhibition> listedExhibitions = new ArrayList<>();
            exhibitionVoResponses.remove(0);
            for (ExhibitionVoResponse exhibitionVoRespons : exhibitionVoResponses) {
                ExhibitionVoResponse.ListedExhibition listedExhibition = new ExhibitionVoResponse.ListedExhibition();
                listedExhibition.setId(exhibitionVoRespons.getHeroExhibitionId());
                listedExhibition.setCover(exhibitionVoRespons.getHeroExhibitionCover());

                listedExhibition.setName(exhibitionVoRespons.getHeroExhibitionName());
                listedExhibition.setCity(exhibitionVoRespons.getCity());

                listedExhibition.setSort(exhibitionVoRespons.getSort());
                listedExhibition.setAppointmentStatus(exhibitionVoRespons.getAppointmentStatus());
                listedExhibitions.add(listedExhibition);
            }
            exhibitionVoResponse.setListedExhibitions(listedExhibitions);
        }
        CourseFilterRequest request = new CourseFilterRequest();
        request.setIsSelect(true);
        exhibitionVoResponse.setCourseListVos(courseService.getListNoPage(language,request));
        return exhibitionVoResponse;
    }

    public List<ExhibitionVoResponse> getExhibitions(String language,List<Exhibition> exhibitionList) {
        List<ExhibitionVoResponse> exhibitionVoResponses = new ArrayList<>();
        if (exhibitionList.size() <= 0) {
            return exhibitionVoResponses;
        }
        for (Exhibition exhibition : exhibitionList) {
            ExhibitionVoResponse exhibitionVoResponse = new ExhibitionVoResponse();
            exhibitionVoResponse.setHeroExhibitionId(exhibition.getId());
            exhibitionVoResponse.setHeroExhibitionCover(exhibition.getCover());
            if("en".equals(language)){
                exhibitionVoResponse.setHeroExhibitionName(exhibition.getNameEn());
            }else{
                exhibitionVoResponse.setHeroExhibitionName(exhibition.getName());
            }
            exhibitionVoResponse.setHeroExhibitionDateStart(DateUtil.format(exhibition.getStartTime(), Constants.DATE_FORMAT_DATE));
            exhibitionVoResponse.setHeroExhibitionDateEnd(DateUtil.format(exhibition.getEndTime(), Constants.DATE_FORMAT_DATE));
            HashMap<String, Object> addressMap = systemGroupDataService.getMapByGid(SysGroupDataConstants.GROUP_DATA_ID_CLIENT_COURSE_ADDRESS, exhibition.getCourseAddressId());
            Object courseAddress = "";
            Object courseAddressInfo = "";
            if("en".equals(language)){
                courseAddress = addressMap.get("courseAddressEn");
                courseAddressInfo = addressMap.get("courseAddressInfoEn");
            }else{
                courseAddress = addressMap.get("courseAddress");
                courseAddressInfo = addressMap.get("courseAddressInfo");
            }
            Object lat = addressMap.get("lat");
            Object lon = addressMap.get("lon");
            Object dest = "";
            if("en".equals(language)){
                dest = addressMap.get("destEn");
            }else{
                dest = addressMap.get("dest");
            }
            HashMap<String, Object> address = new HashMap<>();
            address.put("courseAddress", courseAddress);
            address.put("courseAddressInfo", courseAddressInfo);
            address.put("lat", lat);
            address.put("lon", lon);

            address.put("dest", dest);
            exhibitionVoResponse.setAddress(address);
            if("en".equals(language)){
                exhibitionVoResponse.setCity(exhibition.getCityNameEn());
            }else{
                exhibitionVoResponse.setCity(exhibition.getCityName());
            }
            exhibitionVoResponse.setSort(exhibition.getSort());
            exhibitionVoResponse.setAppointmentStatus(exhibition.getAppointmentStatus());
            exhibitionVoResponses.add(exhibitionVoResponse);
        }
        return exhibitionVoResponses;
    }

    /**
     * 修改排序
     *
     * @param exhibitionId 展览id
     * @param sort         排序
     * @return
     * @author Li
     * @date 2022/11/21 10:12
     */
    @Override
    public boolean updateSort(Integer exhibitionId, Integer sort) {
        LambdaQueryWrapper<Exhibition> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Exhibition::getId, exhibitionId);
        wrapper.eq(Exhibition::getIsDeleted, 0);
        Exhibition exhibition = dao.selectOne(wrapper);
        if (ObjectUtil.isEmpty(exhibition)) {
            throw new VcaException("该展览不存在");
        }
        exhibition.setSort(sort);
        return transactionTemplate.execute(e -> {
            dao.updateById(exhibition);
            return Boolean.TRUE;
        });
    }

    /**
     * 修改限量
     *
     * @param exhibitionId 展览id
     * @param seatCount    限量
     * @return
     * @author Li
     * @date 2022/11/21 10:12
     */
    @Override
    public boolean updateSeatCount(Integer exhibitionId, Integer seatCount) {
        LambdaQueryWrapper<Exhibition> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Exhibition::getId, exhibitionId);
        wrapper.eq(Exhibition::getIsDeleted, 0);
        Exhibition exhibition = dao.selectOne(wrapper);
        if (ObjectUtil.isEmpty(exhibition)) {
            throw new VcaException("该展览不存在");
        }
        exhibition.setQuota(seatCount);
        return transactionTemplate.execute(e -> {
            dao.updateById(exhibition);
            return Boolean.TRUE;
        });
    }

    /**
     * 上架展览
     *
     * @param exhibitionId 展览id
     * @return
     * @author Li
     * @date 2022/11/21 10:12
     */
    @Override
    public boolean courseOnShell(Integer exhibitionId) {
        LambdaQueryWrapper<Exhibition> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Exhibition::getId, exhibitionId);
        wrapper.eq(Exhibition::getIsDeleted, 0);
        Exhibition exhibition = dao.selectOne(wrapper);
        if (ObjectUtil.isEmpty(exhibition)) {
            throw new VcaException("该展览不存在");
        }
        exhibition.setStatus(1);
        return transactionTemplate.execute(e -> {
            dao.updateById(exhibition);
            return Boolean.TRUE;
        });
    }

    /**
     * 下架展览
     *
     * @param exhibitionId 展览id
     * @return
     * @author Li
     * @date 2022/11/21 10:12
     */
    @Override
    public boolean courseOffShell(Integer exhibitionId) {
        LambdaQueryWrapper<Exhibition> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Exhibition::getId, exhibitionId);
        wrapper.eq(Exhibition::getIsDeleted, 0);
        Exhibition exhibition = dao.selectOne(wrapper);
        if (ObjectUtil.isEmpty(exhibition)) {
            throw new VcaException("该展览不存在");
        }
        exhibition.setStatus(0);
        return transactionTemplate.execute(e -> {
            dao.updateById(exhibition);
            return Boolean.TRUE;
        });
    }

    /**
     * 添加展览
     *
     * @param request
     * @return
     * @author Li
     * @date 2022/11/21 10:12
     */
    @Override
    public boolean addTalk(ExhibitionAddRequest request) {

        //去除阿里云前缀
        List<String> carouselMaps = new ArrayList();
        request.getCarouselMap().forEach(e -> {
            carouselMaps.add(systemAttachmentService.clearPrefix(e));
        });
        Exhibition exhibition = new Exhibition();
        BeanUtils.copyProperties(request, exhibition);
        exhibition.setStatus(0);
        if (ObjectUtil.isEmpty(exhibition.getSort())) {
            exhibition.setSort(0);
        }
        //获得分割后的展览日期
        List<String> time = VcaUtil.stringToArrayStr(request.getTime());
        //获得分割后的展览时间(小时)
        List<String> hours = VcaUtil.stringToArrayStr(request.getHours());
        //赋值
        exhibition.setStartTime(com.vca.common.utils.DateUtil.strToDate(time.get(0), "yyyy-MM-dd"));
        exhibition.setEndTime(com.vca.common.utils.DateUtil.strToDate(time.get(1), "yyyy-MM-dd"));
        exhibition.setStartHours(hours.get(0));
        exhibition.setEndHours(hours.get(1));
        //去除阿里云前缀
        exhibition.setCover(systemAttachmentService.clearPrefix(request.getCoverImage()));
        exhibition.setCarouselMap(systemAttachmentService.clearPrefix(JSONArray.parseArray(JSON.toJSONString(carouselMaps)).toJSONString()));
        exhibition.setMainPictureVideo(StringUtils.isNotBlank(request.getMainPictureVideo()) ? systemAttachmentService.clearPrefix(request.getMainPictureVideo()) : "");
        exhibition.setSharePoster(systemAttachmentService.clearPrefix(request.getSharePoster()));
        exhibition.setAppointmentStatus(request.getAppointmentStatus());
        exhibition.setPrice(new BigDecimal(request.getPrice()));
        List<CourseAbout> courseAboutList = new ArrayList();
        //将相关课程和商品根据0 = 课程 1=讲座 2=展览 3=商品 进行分组
        Map<Integer, List<CourseAboutAddRequest>> listMap = request.getCourseAboutAddRequests().stream().collect(Collectors.groupingBy(e -> e.getTypeCourse()));

        //验证判断添加的相关课程和商品是否存在
        courseAboutService.checkWhetherCourseAbout(listMap);

        //添加课程详情
        ExhibitionInfo exhibitionInfo = new ExhibitionInfo();
        exhibitionInfo.setDetails(systemAttachmentService.clearPrefix(request.getDetails()));
        exhibitionInfo.setDetailsEn(systemAttachmentService.clearPrefix(request.getDetailsEn()));
        exhibitionInfo.setIsDeleted(0);
        //事务
        return transactionTemplate.execute(e -> {
            //添加套课并获得 套课id
            save(exhibition);
            List<CoursePackageRelation> coursePackageRelations = new ArrayList();
            //添加套课和相关课程或商品的关系
            request.getCourseAboutAddRequests().forEach(g -> {
                CourseAbout courseAbout = new CourseAbout();
                BeanUtils.copyProperties(g, courseAbout);
                if (ObjectUtil.isEmpty(g.getSort())) {
                    courseAbout.setSort(0);
                }
                courseAbout.setTypeMainCourse(3);
                courseAbout.setAboutMainId(exhibition.getId());
                courseAboutList.add(courseAbout);
            });
            courseAboutService.addCourseAboutList(courseAboutList);
            exhibitionInfo.setExhibitionId(exhibition.getId());
            exhibitionInfoService.save(exhibitionInfo);
            return Boolean.TRUE;
        });
    }

    /**
     * 根据id获取展览信息
     *
     * @param exhibitionId
     * @return
     * @author Li
     * @date 2022/11/22 10:25
     */
    @Override
    public ExhibitionAdminResponse getTalk(Long exhibitionId) {
        ExhibitionAdminResponse response = new ExhibitionAdminResponse();
        //查询课程是否存在
        LambdaQueryWrapper<Exhibition> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Exhibition::getId, exhibitionId);
        wrapper.eq(Exhibition::getIsRecycle, 0);
        Exhibition exhibition = dao.selectOne(wrapper);
        if (ObjectUtil.isEmpty(exhibition)) {
            throw new VcaException("课程不存在");
        }
        BeanUtils.copyProperties(exhibition, response);
        response.setCover(exhibition.getCover());
        response.setCoverImage(exhibition.getCover());
        List<String> time = new ArrayList<String>();
        time.add(com.vca.common.utils.DateUtil.dateToStr(exhibition.getStartTime(), "yyyy-MM-dd"));
        time.add(com.vca.common.utils.DateUtil.dateToStr(exhibition.getEndTime(), "yyyy-MM-dd"));
        response.setTime(time);
        List<String> hours = new ArrayList<>();
        hours.add(exhibition.getStartHours());
        hours.add(exhibition.getEndHours());
        response.setHours(hours);
        response.setChannelsId(exhibition.getChannelsId());
        response.setVideoId(exhibition.getVideoId());
        List<CourseAbout> courseAbouts = courseAboutService.list(new LambdaQueryWrapper<CourseAbout>().eq(CourseAbout::getAboutMainId, exhibitionId).eq(CourseAbout::getIsDeleted, 0).eq(CourseAbout::getTypeMainCourse, 3));
        //将相关课程和商品根据0 = 课程 1=讲座 2=展览 3=商品 4=套课
        Map<Integer, List<CourseAbout>> listMap = courseAbouts.stream().collect(Collectors.groupingBy(e -> e.getTypeCourse()));
        //验证判断添加的相关课程和商品是否存在
        Map<String, List<HashMap<String, Object>>> courseAdminConfig = systemGroupDataService.getCourseAdminConfig();
        List<HashMap<String, Object>> address = courseAdminConfig.get("addressList");
        //获得地址信息
        address.forEach(e -> {
            Map map = JSON.parseObject(JSONObject.toJSONString(e), Map.class);
            if (map.get("id").equals(exhibition.getCourseAddressId())) {
                response.setCourseAddress(map.get("courseAddress").toString() + map.get("courseAddressInfo").toString());
            }
        });
        //课程详情
        ExhibitionInfo exhibitionInfo = exhibitionInfoService.getOne(new LambdaQueryWrapper<ExhibitionInfo>().eq(ExhibitionInfo::getExhibitionId, exhibitionId));
        response.setDetails(exhibitionInfo.getDetails());
        response.setDetailsEn(exhibitionInfo.getDetailsEn());
        //获得相关课程的信息
        List<CourseAboutAdminResponse> aboutList = courseAboutService.getAdminCourseAbout(listMap, courseAdminConfig);
        response.setCourseAboutAdminResponses(aboutList);
        return response;
    }

    /**
     * @param request
     * @Description:修改展览
     * @Author: Li
     * @Date: 2022/11/22 14:56
     */
    @Override
    public boolean updateTalk(ExhibitionAddRequest request) {
        //去除阿里云前缀
        List<String> carouselMaps = new ArrayList();
        request.getCarouselMap().forEach(e -> {
            carouselMaps.add(systemAttachmentService.clearPrefix(e));
        });
        Exhibition exhibition = dao.selectOne(new LambdaQueryWrapper<Exhibition>().eq(Exhibition::getId, request.getId()).eq(Exhibition::getIsDeleted, 0));
        if (ObjectUtil.isEmpty(exhibition)) {
            throw new VcaException("该课程不存在");
        }
        BeanUtils.copyProperties(request, exhibition);
        exhibition.setStatus(0);
        if (ObjectUtil.isEmpty(exhibition.getSort())) {
            exhibition.setSort(0);
        }
        //获得分割后的展览日期
        List<String> time = VcaUtil.stringToArrayStr(request.getTime());
        //获得分割后的展览时间(小时)
        List<String> hours = VcaUtil.stringToArrayStr(request.getHours());
        //赋值
        exhibition.setStartTime(com.vca.common.utils.DateUtil.strToDate(time.get(0), "yyyy-MM-dd"));
        exhibition.setEndTime(com.vca.common.utils.DateUtil.strToDate(time.get(1), "yyyy-MM-dd"));
        exhibition.setStartHours(hours.get(0));
        exhibition.setEndHours(hours.get(1));
        //去除阿里云前缀
        exhibition.setCover(systemAttachmentService.clearPrefix(request.getCoverImage()));
        exhibition.setCarouselMap(systemAttachmentService.clearPrefix(JSONArray.parseArray(JSON.toJSONString(carouselMaps)).toJSONString()));
        exhibition.setMainPictureVideo(StringUtils.isNotBlank(request.getMainPictureVideo()) ? systemAttachmentService.clearPrefix(request.getMainPictureVideo()) : "");
        exhibition.setSharePoster(systemAttachmentService.clearPrefix(request.getSharePoster()));
        exhibition.setPrice(new BigDecimal(request.getPrice()));
        List<CourseAbout> courseAboutList = new ArrayList();
        //将相关课程和商品根据0 = 课程 1=讲座 2=展览 3=商品 4=套课 进行分组
        Map<Integer, List<CourseAboutAddRequest>> listMap = request.getCourseAboutAddRequests().stream().collect(Collectors.groupingBy((CourseAboutAddRequest::getTypeCourse)));

        //校验添加的相关课程是否存在
        courseAboutService.checkWhetherCourseAbout(listMap);

        //修改展览详情
        ExhibitionInfo exhibitionInfo = exhibitionInfoService.getOne(new LambdaQueryWrapper<ExhibitionInfo>().eq(ExhibitionInfo::getExhibitionId, request.getId()));
        exhibitionInfo.setDetails(systemAttachmentService.clearPrefix(request.getDetails()));
        exhibitionInfo.setDetailsEn(systemAttachmentService.clearPrefix(request.getDetailsEn()));
        request.getCourseAboutAddRequests().forEach(g -> {
            CourseAbout courseAbout = new CourseAbout();
            BeanUtils.copyProperties(g, courseAbout);
            if (ObjectUtil.isEmpty(g.getSort())) {
                courseAbout.setSort(0);
            }
            courseAbout.setAboutMainId(request.getId());
            courseAbout.setTypeMainCourse(3);
            if (ObjectUtil.isNotEmpty(g.getCourseAboutId())) {
                courseAbout.setId(g.getCourseAboutId());
            }
            courseAboutList.add(courseAbout);
        });
        //事务
        return transactionTemplate.execute(e -> {
            //添加课程并获得 课程id
            updateById(exhibition);
            //添加套课和相关课程或商品的关系
            courseAboutService.updateCourseAboutList(courseAboutList, request.getId());
            exhibitionInfoService.updateById(exhibitionInfo);
            return Boolean.TRUE;
        });
    }

    /**
     * 获得展览头部数据
     *
     * @return
     * @author Li
     * @date 2022/11/23 09:16
     */
    @Override
    public List<AdminHeaderResponse> getHeader() {
        List<AdminHeaderResponse> headers = new ArrayList();
        AdminHeaderResponse active = new AdminHeaderResponse(0, "Active", 1, null);
        AdminHeaderResponse inActive = new AdminHeaderResponse(0, "InActive", 0, null);
        active.setCount(dao.selectList(new LambdaQueryWrapper<Exhibition>().eq(Exhibition::getStatus, 1).eq(Exhibition::getIsDeleted, 0)).size());
        inActive.setCount(dao.selectList(new LambdaQueryWrapper<Exhibition>().eq(Exhibition::getStatus, 0).eq(Exhibition::getIsDeleted, 0)).size());
        headers.add(active);
        headers.add(inActive);
        return headers;
    }

    /**
     * 删除展览
     *
     * @param exhibitionId
     * @return
     * @author Li
     * @date 2022/11/23 09:16
     */
    @Override
    public boolean deleteTalk(Long exhibitionId) {
        Exhibition exhibition = checkExhibition(exhibitionId);
        List<ExhibitionScheduling> schedulings = exhibitionSchedulingService.list(new LambdaQueryWrapper<ExhibitionScheduling>().eq(ExhibitionScheduling::getExhibitionId, exhibition.getId()).eq(ExhibitionScheduling::getIsDeleted, 0));
        if (ObjectUtil.isNotEmpty(schedulings)) {
            throw new VcaException("请先删除该展览的排期");
        }
        List<CourseAbout> abouts = courseAboutService.list(new LambdaQueryWrapper<CourseAbout>().eq(CourseAbout::getAboutMainId, exhibitionId).eq(CourseAbout::getIsDeleted, 0));

        exhibition.setIsDeleted(1);
        return transactionTemplate.execute(e -> {
            courseAboutService.delByids(abouts.stream().map(CourseAbout::getId).collect(Collectors.toList()));
            dao.update(exhibition, new LambdaUpdateWrapper<Exhibition>().eq(Exhibition::getId, exhibitionId).set(Exhibition::getIsDeleted, 1));
            return Boolean.TRUE;
        });
    }

    @Override
    public List<Exhibition> getExhibitionsAndDelete(List<Long> ids) {
        return dao.getExhibitions(ids);
    }

    /**
     * @Description:查询当前月份的日程
     * @author:chenbing
     * @date 2023/1/28 16:06
     */
    @Override
    public List<Integer> getCalendar(Integer month, Integer year) {
        List<Exhibition> exhibitions = list(new LambdaQueryWrapper<Exhibition>().eq(Exhibition::getIsDeleted, 0).eq(Exhibition::getStatus, 1).eq(Exhibition::getIsRecycle, 0));
        if (exhibitions.size() <= 0) {
            return null;
        }
        List<Long> exhibitionIds = exhibitions.stream().map(Exhibition::getId).collect(Collectors.toList());
        LambdaQueryWrapper<ExhibitionScheduling> lqw = new LambdaQueryWrapper<>();
        lqw.select(ExhibitionScheduling::getId, ExhibitionScheduling::getExhibitionId, ExhibitionScheduling::getSchedulingDate);
        lqw.eq(ExhibitionScheduling::getIsDeleted, 0).eq(ExhibitionScheduling::getIsCancel, 0).eq(ExhibitionScheduling::getStatus, 1);
        lqw.ge(ExhibitionScheduling::getSchedulingDate, com.vca.common.utils.DateUtil.getMonthStartDay(month, year));
        lqw.le(ExhibitionScheduling::getSchedulingDate, com.vca.common.utils.DateUtil.getMonthEndDay(month, year));
        lqw.in(ExhibitionScheduling::getExhibitionId, exhibitionIds);
        lqw.groupBy(ExhibitionScheduling::getSchedulingDate).orderByAsc(ExhibitionScheduling::getSchedulingDate);
        List<ExhibitionScheduling> schedulingList = exhibitionSchedulingService.list(lqw);
        List<Integer> dates = new ArrayList<>();
        for (ExhibitionScheduling exhibitionScheduling : schedulingList) {
            Date date = com.vca.common.utils.DateUtil.strToDate(exhibitionScheduling.getSchedulingDate(), Constants.DATE_FORMAT_DATE);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int day = calendar.get(Calendar.DATE);
            dates.add(day);
        }
        return dates;
    }

    private Exhibition checkExhibition(Long exhibitionId) {
        LambdaQueryWrapper<Exhibition> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Exhibition::getId, exhibitionId);
        wrapper.eq(Exhibition::getIsDeleted, 0);
        Exhibition exhibition = dao.selectOne(wrapper);
        if (ObjectUtil.isEmpty(exhibition)) {
            throw new VcaException("该课程不存在");
        }
        return exhibition;
    }
}
